package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"iot-base/common/logger"
	"iot-base/common/notify"
	"time"
)

func Edge(c *gin.Context) {
	now := time.Now().Unix()
	params := c.Request.URL.Query()
	res, _ := ioutil.ReadAll(c.Request.Body)
	SN := params.Get("SN")
	fmt.Println("SN:", SN)
	for k, v := range params {
		switch k {
		case "table":
			for i := 0; i < len(v); i++ {
				switch v[i] {
				case "rtstate":
					logger.Log.Info("rtstate:" + string(res))
					//re := DealBodyrt(string(res))
					//sta, err := strconv.ParseInt(re["relay"], 8, 0)
					//if err != nil {
					//	fmt.Println("parse relay!",err)
					//	sta = 0
					//}
					//fmt.Printf("id:%s    pid: rtstate   value:%s",Registtable[SN].devid,re["relay"])
					//notify.RawData(Registtable[SN].devid, "rtstate", "", 0, re["relay"], now, now)
					c.Writer.Write([]byte("OK"))
				case "rtlog":
					logger.Log.Info("rlog:" + string(res))
					re := DealBodyrt(string(res))

					var status string
					var msgstatus string
					if re["eventaddr"] == "2" {
						status = "1"
						msgstatus = "开"
					} else if re["eventaddr"] == "1" {
						status = "2"
						msgstatus = "关"
					} else {
						status = "0"
						msgstatus = ""
					}
					eventlog := map[string]interface{}{
						"Name":  re["pin"],
						"Now":   re["time"],
						"Mod":   msgstatus,
						"Event": DealEvent(re["event"]),
					}

					fmt.Println(eventlog)
					notify.RawData(Registtable[SN].devid, "rtstate", "", 0, status, now, now)
					notify.RawEvent(Registtable[SN].devid, "guardLog", time.Now(), eventlog, nil)
					notify.RawData(Registtable[SN].devid, "pin", "", 0, re["pin"], now, now)
					notify.RawData(Registtable[SN].devid, "rlog", "", 0, re["event"], now, now)
					notify.RawData(Registtable[SN].devid, "status", "", 0, re["inoutstatus"], now, now)
					c.Writer.Write([]byte("OK"))
				case "user":
					fmt.Println(string(res))
					logger.Log.Info("user:" + string(res))
					c.Writer.Write([]byte("OK"))
				default:
					c.Writer.Write([]byte("OK"))
				}
			}
			return
		}
	}
}

func DealEvent(data string) string {
	v, ok := event[data]
	if ok {
		return v
	} else {
		return data
	}
}

func DealStatus(data string) string {
	v, ok := state[data]
	if ok {
		return v
	} else {
		return data
	}
}

var state map[string]string = map[string]string{
	"0": "入",
	"1": "出",
}

var event map[string]string = map[string]string{
	"0":   "正常刷卡开门",
	"1":   "常开时间段内刷卡",
	"2":   "首人开门",
	"3":   "多人开门",
	"4":   "紧急状态密码开门",
	"5":   "常开时间段开门",
	"6":   "触发联动",
	"7":   "取消报警",
	"8":   "远程开门",
	"9":   "远程关门",
	"10":  "禁用当天常开时间段",
	"11":  "启用当天常开时间段",
	"12":  "辅助输出远程打开",
	"13":  "辅助输出远程关闭",
	"14":  "正常按指纹开门",
	"15":  "多人开门",
	"16":  "常开时间段内按指纹",
	"17":  "正常验证开门",
	"18":  "首人开门",
	"19":  "首人开门",
	"20":  "操作间隔太短",
	"21":  "门非有效时间段",
	"22":  "非法时间段",
	"23":  "非法访问",
	"24":  "反潜",
	"25":  "互锁",
	"26":  "多人验证等待",
	"27":  "卡未注册",
	"28":  "门开超时",
	"29":  "卡已过有效期",
	"30":  "密码错误",
	"31":  "按指纹间隔太短",
	"32":  "多人验证等待",
	"33":  "人已过有效期",
	"34":  "人未登记",
	"35":  "门非有效时间段验证开门",
	"36":  "门非有效时间段",
	"37":  "常开时间段无法关门",
	"38":  "卡已挂失",
	"39":  "黑名单",
	"40":  "多人验证失败",
	"41":  "验证方式错误",
	"42":  "韦根格式错误",
	"44":  "后台验证失败",
	"45":  "后台验证超时",
	"47":  "发送命令失败",
	"48":  "多人验证失败",
	"49":  "门非有效时间段",
	"50":  "操作间隔太短",
	"51":  "多人验证等待",
	"52":  "多人验证失败",
	"53":  "人已过有效期",
	"54":  "电池电压过低",
	"55":  "立即更换电池",
	"56":  "非法操作",
	"57":  "后备电源",
	"58":  "常开报警",
	"59":  "非法管理",
	"60":  "门被反锁",
	"61":  "重复验证",
	"62":  "禁止用户",
	"63":  "门已锁定",
	"64":  "出门按钮不在时间段内操作",
	"65":  "辅助输入不在时间段内操作",
	"66":  "读头升级失败",
	"67":  "远程比对成功",
	"68":  "体温异常不通行",
	"69":  "口罩异常不通行",
	"70":  "人脸比对服务器通信异常",
	"71":  "人脸服务器响应异常",
	"72":  "呼叫无应答",
	"73":  "二维码无效",
	"74":  "二维码已过期",
	"75":  "组合验证超时",
	"76":  "TITAN模式下取不到指纹",
	"77":  "没插上网线",
	"78":  "设备热点异常断开",
	"79":  "移动网络异常断开",
	"100": "防拆报警",
	"101": "胁迫密码开门",
	"102": "门被意外打开",
	"103": "胁迫指纹开门",
	"104": "无效卡刷卡报警",
	"105": "网络掉线",
	"106": "电池掉电",
	"107": "市电掉电",
	"108": "无法连接主控",
	"109": "读头拆机报警",
	"110": "读头离线报警",
	"111": "POE掉电",
	"112": "扩展板离线报警",
	"113": "非法安全等级",
	"114": "线路检测，火警输入断开",
	"115": "线路检测，火警输入短路",
	"116": "线路检测，辅助输入断开",
	"117": "线路检测，辅助输入短路",
	"118": "线路检测，出门开关断开",
	"119": "线路检测，出门开关短路",
	"120": "线路检测，门磁断开",
	"121": "线路检测，门磁短路",
	"200": "门已打开",
	"201": "门已关闭",
	"202": "出门按钮开门",
	"203": "多人开门",
	"204": "门常开时间段结束",
	"205": "远程开门常开",
	"206": "设备启动",
	"207": "密码开门",
	"208": "超级用户开门",
	"209": "触发出门按钮",
	"210": "启动消防开门",
	"211": "超级用户关门",
	"212": "开启电梯控制功能",
	"213": "关闭电梯控制功能",
	"214": "成功连接服务器",
	"215": "首卡开门",
	"216": "常开时间段内按密码",
	"217": "成功连接主控",
	"218": "身份证通行",
	"219": "出门按钮常开时间段内验证",
	"220": "辅助输入点断开",
	"221": "辅助输入点短路",
	"222": "后台验证成功",
	"223": "后台验证",
	"224": "按门铃",
	"225": "辅助输入点正常",
	"226": "辅助输入点触发",
	"227": "门双开",
	"228": "门双关",
	"229": "辅助输出定时常开",
	"230": "辅助输出定时关闭常开",
	"231": "IPC固件联动事件",
	"232": "验证通过",
	"233": "远程锁定",
	"234": "远程解锁",
	"235": "读头升级成功",
	"236": "读头拆机报警解除",
	"237": "读头在线",
	"239": "设备呼叫",
	"240": "通话结束",
	"241": "联动抓拍事件",
	"242": "联动录像事件",
	"243": "火警输入断开",
	"244": "火警输入短路",
	"245": "连上热点",
	"246": "移动网络连接成功",
	"247": "扩展板在线",
	"248": "插上网线",
	"254": "扩展事件编号定义",
	"255": "门状态",
}
